package com.itheima.web.servlet;

import cn.hutool.core.io.IoUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.BeanFactoryUtils;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@MultipartConfig
@WebServlet(name = "UserServlet" , urlPatterns = "/user")
public class UserServlet extends BaseServlet {
    protected void saveUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletInputStream is = request.getInputStream();
        //读取数据
        User user = new ObjectMapper().readValue(is, User.class);
        UserService userService = (UserService) BeanFactoryUtils.getBean("userService");
        userService.save(user);

        response.getWriter().print("ok");
    }
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletInputStream is = request.getInputStream();
        //读取数据
        User user = new ObjectMapper().readValue(is, User.class);

        UserService userService = (UserService) BeanFactoryUtils.getBean("userService");
        User loginUser = userService.login(user.getUsername(), user.getPassword());

        if(loginUser == null){
            response.getWriter().print("error");
        }else{
            request.getSession().setAttribute("user" , loginUser);
            response.getWriter().print("ok");
        }


    }


    protected void checkUserLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object user = request.getSession().getAttribute("user");
        if(user != null){
            String json = new ObjectMapper().writeValueAsString(user);
            response.getWriter().print(json);
            System.out.println("获得登录数据:"+json);
        }else{
            response.getWriter().print("1");
        }
    }

    protected void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
    }




    protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        ServletInputStream is = request.getInputStream();
        User user = new ObjectMapper().readValue(is, User.class);
        System.out.println(user);
        UserService userService = (UserService) BeanFactoryUtils.getBean("userService");
        userService.updateUser(user);
        request.getSession().setAttribute("user" , user);
        response.getWriter().print("ok");
    }
    protected void updateUserPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        Part pic = request.getPart("pic");
        if(pic!= null && pic.getSize()>0){
            String realPath = getServletContext().getRealPath("/pic");
            InputStream is = pic.getInputStream();
            FileOutputStream os = new FileOutputStream(realPath  + "/" + pic.getSubmittedFileName());
            IoUtil.copy(is , os);

            is.close();
            os.close();
            User loginUser =(User) request.getSession().getAttribute("user");
            loginUser.setPic( "/pic/" + pic.getSubmittedFileName() );
            UserService userService = (UserService) BeanFactoryUtils.getBean("userService");
            userService.updateUser(loginUser);

            request.getSession().setAttribute("user" ,loginUser );
        }


       response.sendRedirect("/home_index.html");
    }




}
